package com.lmaos.basetools.control;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.lmaos.allcontrol.beancontrol.entity_.LMClassParam;
import com.lmaos.allcontrol.beancontrol.utils_.ObjUtils;
import com.lmaos.basetools.sText.TextUtils;

public class SControlImpl implements SControl{
	Map<String, SControlBean> controlBeans = new ConcurrentHashMap<String, SControlBean>(128);
	Map<String, SControlInterceptBean> controlInterceptBeans = new ConcurrentHashMap<String, SControlInterceptBean>(128);
	Map<String, SControlResultBean> controlResultBeans = new ConcurrentHashMap<String, SControlResultBean>(128);
	String root;
	private String contentType;		// 文本类型
	private String charset = "UTF-8";// 设置文本编码
	
	public SControlImpl(String root) {
		super();
		this.root = root;
	}
	@Override
	public void addControl(Object controlObj) {
		SControlUtils.loadSControl(controlObj, 
				controlBeans, 
				controlInterceptBeans, 
				controlResultBeans,charset,contentType);
	}
	@Override
	public void addControls(List<Object> controlObjs) {
		for (Object controlObj : controlObjs) {
			addControl(controlObj);
		}
	}
	
	public String getSControlBeanEncode(String url){
		SControlBean controlBean = getSControlBean(url);
		if(controlBean == null){
			return charset;
		}
		return controlBean.getEncode();
	}
	public String getSControlBeanContentType(String url){
		SControlBean controlBean = getSControlBean(url);
		if(controlBean == null){
			return contentType;
		}
		return controlBean.getContentType();
	}
	public SControlBean getSControlBean(String url){
		url = SControlUtils.subUrl(getRoot(), url);
		SControlBean controlBean = controlBeans.get(url);
		return controlBean;
	}
	
	@Override
	public int execute(LMClassParam classParam, String url) {
		
		SControlBean controlBean = getSControlBean(url);
		if(controlBean == null){
			return REQ_NOT_EXIST;
		}
		return execute(classParam, controlBean);
	}
	
	@Override
	public int execute(LMClassParam classParam, SControlBean controlBean) {
		if(controlBean == null){
			return REQ_NOT_EXIST;
		}
		SControlExec controlExec = new SControlExec(controlBean);
		SResult result = controlExec.getResult();
		try {
			
			String interceptNames[] = controlBean.getInterceptNames();
			String resultName = controlBean.getResultName();
			classParam.put(controlExec);
			classParam.put(result);
			try {
				for (String interceptName : interceptNames) {
					SControlInterceptBean interceptBean = controlInterceptBeans.get(interceptName);
					if(interceptBean!=null){
						controlExec.notexec();
						result.setResult(ObjUtils.executeMethod(interceptBean.getControlInterceptObj(), interceptBean.getControlInterceptMethod(), classParam));
						if(controlExec.isNotExec()){
							resultName = TextUtils.ifIsBlankSetValue(interceptBean.getResultName(),resultName);
							controlExec.setControlInterceptBean(interceptBean);
							break; 
						}
					}
				}
				if(controlExec.isExec()){
						result.setResult(ObjUtils.executeMethod(controlBean.getControlObj(), controlBean.getControlMethod(), classParam));
					
				}
			} catch (Exception e) {
				result.setE(e);
			}
			// 结果执行 返回值不为 void 时候
			if(controlBean.getControlMethod().getReturnType()!=void.class||controlExec.isIntercept()&&controlExec.getControlInterceptBean().getControlInterceptMethod().getReturnType()!=void.class){
				executeResult(classParam, resultName);
			}else if(result.getE()!=null){
				throw result.getE();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return REQ_SERVER_ERR;
		}
		
		return REQ_SUCCESS_200;
	}
	
	@Override
	public void executeResult(LMClassParam classParam, SControlResultBean resultBean) throws Exception {
		if(resultBean!=null){
			ObjUtils.executeMethod(resultBean.getControlResultObj(), resultBean.getControlResultMethod(), classParam);
		}
	}
	@Override
	public void executeResult(LMClassParam classParam, String resultName) throws Exception{
		SControlResultBean resultBean = controlResultBeans.get(resultName);
		executeResult(classParam, resultBean);
	}
	
	@Override
	public String getRoot() {
		
		return root;
	}
	@Override
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	@Override
	public void setEncode(String charset) {
		this.charset = TextUtils.ifIsBlankSetValue(charset,this.charset);
	}
	@Override
	public List<SControlBean> getScontrolBeans() {
		
		return new ArrayList<SControlBean>(controlBeans.values());
	}
}
